<?php

namespace app\common\service\FinancialService;

use app\model\BbcModel\ChannelBusiness;
use app\model\BbcModel\MasterIncome;
use app\model\BbcModel\Commission;
use app\model\BbcModel\ServiceProvider;
use app\model\BbcModel\Store;
use app\Normal;


/**
 * 平台结算统计
 * @author Administrator
 */
class MasterStatisticsService
{

    /**
     * 平台结算统计
     * @param array $param
     * @return array
     */
    public function orderList(array $param)
    {
        $info=MasterIncome::instance()->info(['id'=>1]);
        $info['wallet_accumulate_income']=$info['wallet_accumulate_income']??0;
        $info['frozen_income']=$info['frozen_income']??0;
        $where=[
           ['status','=',1]
        ];
        //服务商ID
        if(!empty($param['sp_id'])){
            $where[]=['sp_id','=',$param['sp_id']];
        }
        //子订单号
        if(!empty($param['sn'])){
            $where[]=['sn','like','%'.$param['sn'].'%'];
        }
        //下单时间
        if(!empty($param['cd_start'])){
            $where[]=['md','>=',strtotime($param['cd_start'])];
        }
        if(!empty($param['cd_end'])){
            $where[]=['md','<=',strtotime($param['cd_end'])];
        }
        if(!empty($param['order_type'])){
            $where[]=['order_type','=',$param['order_type']];
        }
        $page=$param['page']??1;
        $limit=$param['limit']??10;
        $p=($page-1)*$limit;
        $list=Commission::instance()
            ->field('id,sn,sum(master_money) as money,md,order_type,sp_id,cb_id,mn_sid')
            ->where($where)
            ->group('order_id')
            ->order('id desc')
            ->limit($p, $limit)
            ->select()
            ->toArray();
        $num=Commission::instance()
            ->where($where)
            ->group('order_id')
            ->count();
        //服务商列表
        $service=ServiceProvider::instance()->lists(['_fields_'=>'id,name']);
        $service=array_column($service,'name','id');
        //渠道商列表
        $business=ChannelBusiness::instance()->lists(['_fields_'=>'id,name']);
        $business=array_column($business,'name','id');
        //店铺列表
        $store=Store::instance()->lists(['_fields_'=>'id,store_name']);
        $store=array_column($store,'store_name','id');
        foreach ($list as $k=>$v){
            $list[$k]['store_name']=$store[$v['mn_sid']]??'';
            $list[$k]['business_name']=$business[$v['cb_id']]??'';
            $list[$k]['service_name']=$service[$v['sp_id']]??'';
            $list[$k]['cd']=date('Y-m-d H:i:s',$v['md']);
            $list[$k]['type']=Commission::instance()->order_type[$v['order_type']];
        }
        $data['info']=$info;
        $data['total']=$num;
        $data['list']=$list;
        return Normal::returnCode(Normal::SUC,"请求成功",$data);
    }

    /**
     * 分佣来源
     * @return array
     */
    public function getType()
    {
        $list=Commission::instance()->order_type;
        $data=[];
        foreach ($list as $k=>$v){
            $item['type']=$k;
            $item['name']=$v;
            $data[]=$item;
        }
        return Normal::returnCode(Normal::SUC,"请求成功",$data);
    }

}
